#pragma once
#include "const.h"
#include "LogicSystem.h"
#include <unordered_map>

class HttpConnection : public std::enable_shared_from_this<HttpConnection> {
    friend class LogicSystem;

public:
    explicit HttpConnection(boost::asio::io_context& io_context);

    void Start();

    boost::asio::ip::tcp::socket& GetSocket();

private:
    void CheckDeadLine();

    void WriteResponse();

    void HandleRequest();

private:
    unsigned char ToHex(unsigned char x);

    unsigned char FromHex(unsigned char x);

    std::string UrlEncode(const std::string& str);

    std::string UrlDecode(const std::string& str);

    void PreParseGetParam();

private:
    tcp::socket _socket;
    beast::flat_buffer _buffer{ 8192 };
    http::request<http::dynamic_body> _request;
    http::response<http::dynamic_body> _response;
    net::steady_timer _deadline{ _socket.get_executor(), std::chrono::seconds(60) };

    std::string _get_url;
    std::unordered_map<std::string, std::string> _get_params;
};
